package com.coolapk.service.impl;

import com.coolapk.entity.User;
import com.coolapk.entity.UserExample;
import com.coolapk.mapper.ArticleMapper;
import com.coolapk.mapper.NoticedMapper;
import com.coolapk.mapper.UserMapper;
import com.coolapk.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    ArticleMapper articleMapper;

    @Resource
    NoticedMapper noticedMapper;

    @Override
    public List<User> login(User user) {
        UserExample userExample = new UserExample();
        UserExample.Criteria createCriteria = userExample.createCriteria();
        if (user.getEmail() == null){
            createCriteria.andPhoneEqualTo(user.getPhone());
        }else{
            createCriteria.andEmailEqualTo(user.getEmail());
        }
        createCriteria.andPasswordEqualTo(user.getPassword());
        return userMapper.selectByExample(userExample);

    }

    @Override
    public User countNum(User user) {
        Integer articleNum = articleMapper.articleNum(user.getId());
        Integer noticedNum = noticedMapper.noticedNum(user.getId());
        Integer beNoticedNum = noticedMapper.benoticedNum(user.getId());
        user.setArticleNum(articleNum);
        user.setNoticedNum(noticedNum);
        user.setBeNoticedNum(beNoticedNum);
        return user;
    }

    @Override
    public List<User> checkUser(String email) {
        UserExample userExample = new UserExample();
        UserExample.Criteria createCriteria = userExample.createCriteria();
        createCriteria.andEmailEqualTo(email);
        return userMapper.selectByExample(userExample);
    }

    @Override
    public Integer register(User user) {
        return userMapper.insertSelective(user);
    }

    @Override
    public Integer updateUserInfo(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User getNewUserInfo(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }
}
